import Vue from 'vue';
import Vuex from 'vuex';
import * as actions from './actions';
import * as getters from './getters';

Vue.use(Vuex);

// 应用初始状态
const state = {
  token: '',
  username: '',
  userInfo: '',
  // 七牛图片域名
  imgDomain: 'http://img.test.duoduocr.cn/'
};

// 定义所需的 mutations
const mutations = {
  set_token (state, token) {
    state.token = token;
    sessionStorage.setItem('token', token);
  },
  del_token (state) {
    state.token = '';
    sessionStorage.removeItem('token');
  },
  set_username (state, username) {
    state.username = username;
    sessionStorage.setItem('username', username);
  },
  del_username (state) {
    state.username = '';
    sessionStorage.removeItem('username');
  },
  set_userInfo (state, userInfo) {
    state.userInfo = userInfo;
    sessionStorage.setItem('userInfo', userInfo);
  },
  del_userInfo (state, userInfo) {
    state.userInfo = userInfo;
    sessionStorage.removeItem('userInfo');
  }
};

// 创建 store 实例
export default new Vuex.Store({
  actions,
  getters,
  state,
  mutations
});
